package com.chw.service.impl;

import com.chw.common.FanHui;
import com.chw.common.keyFh;
import com.chw.mapp.TypeMapp;
import com.chw.model.Key;
import com.chw.model.Type;
import com.chw.model.Value;
import com.chw.model.vo.KeyVo;
import com.chw.model.vo.UserVo;
import com.chw.model.vo.ValueVo;
import com.chw.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TypeServiceImpl implements TypeService {
    @Autowired
    private TypeMapp typeMapp;

    StringBuffer sb=null;
    @Override
    public Object queryType() {
        List<Type> types=typeMapp.queryType();
        sb=new StringBuffer("[");
        int count=0;

        for(int i=0;i<types.size();i++){
            Type type=types.get(i);

            if (type.getPid()!=null&&type.getPid()==0) {
                count++;
                sb.append("{");
                sb.append("\"id\":").append(type.getId()).append(",\"pid\":").append(type.getPid()).append(",\"label\":\"").append(type.getName()).append("\"");
                getChildren(type.getId(),types);
                sb.append("},");
            }
        }
        return sb.substring(0,sb.length()-1)+"]";
    }

    @Override
    public void addType(Type type) {
        type.setCreateDate(new Date());
        type.setIsDel(1);
        typeMapp.addType(type);
    }

    @Override
    public void updateType(Type type) {
        typeMapp.updateType(type);
    }

    @Override
    public void deleteType(Integer id) {
        Integer isDel=2;
        typeMapp.deleteType(id,isDel);
    }

    @Override
    public Object queryKey(UserVo userVo) {


        return new keyFh<>(typeMapp.count(userVo),typeMapp.queryKey(userVo));
    }

    @Override
    public void updateKey(Key key) {
        if (key.getIsDel()!=null){
            key.setIsDel(0);
        }
        if (key.getIsSku()!=null){
          if (key.getIsSku()==1){
              key.setIsSku(2);
          }else {
              key.setIsSku(1);
          }
        }
        typeMapp.updateKey(key);
    }

    @Override
    public void addKey(Key key) {
        key.setCreateDate(new Date());
        key.setIsDel(1);
        typeMapp.addKey(key);
    }

    @Override
    public List<Map> queryChildrenStr() {
        List<Map>list=new ArrayList<>();//返回的数组
        //查询数据
        List<Type>types=typeMapp.queryType();
        //遍历所有的数据
        for (Type t:types) {
            //判断是否为父节点
            Boolean parent=isParent(t.getId(),types);
            if (parent==false){
                Map map=new HashMap();
                map.put("id",t.getId());
                sb=new StringBuffer(t.getName());
                getChildrenName(t.getPid(),types);
                map.put("name",getReversStr(sb));
                list.add(map);
            }
        }
        return list;
    }


/**  根据分类 查询 所有的属性和值

            *{  skuData:[{"name":"颜色",values:[{"value":"红色"},{"name":"绿色"}]}],attrData:[]}
    */
    @Override
    public Object queryKeyValue(Integer id) {
        Map map=new HashMap();
        List<ValueVo> isSku=new ArrayList<>();
        List<ValueVo> key=new ArrayList<>();
        //根据tid 查询所有属性
        List<ValueVo>  valueVos=typeMapp.queryData(id);
        for (ValueVo v:valueVos) {
            ValueVo valueVo=v;
            List<Value>values=typeMapp.queryValueDate(valueVo.getId());
            if (valueVo.getTypes().equals(3)){
                valueVo.setVals(new ArrayList<>());
            }else {
                valueVo.setVals("");
            }
            valueVo.setValues(values);
            if (valueVo.getIsSku()==2){
                key.add(valueVo);
            }else {
                isSku.add(valueVo);
            }
        }
        map.put("skuData",isSku);
        map.put("attrData",key);
        return map;
    }




    private Object getReversStr(StringBuffer sb) {
        StringBuffer stringBuffer=new StringBuffer();
        String[]split=sb.toString().split("/");
        List<String>strings= Arrays.asList(split);
        Collections.reverse(strings);
        for (int i=0;i<strings.size();i++){
            stringBuffer.append(strings.get(i)).append("/");
        }
        return stringBuffer;
    }

    //父节点name拼到sb
    private void getChildrenName(Integer pid, List<Type> types) {
        for (int i=0;i<types.size();i++){
            Type type=types.get(i);
            if (type.getId().equals(pid)){
                sb.append("/").append(type.getName());
                if (!type.getPid().equals(0)){
                    getChildrenName(type.getPid(),types);
                }
                break;
            }
        }
    }

    //判断传入的id是否有子节点
    private Boolean isParent(Integer id, List<Type> types) {
        for (Type t:types) {
            if (t.getPid().equals(id)){
                return true;
            }
        }
        return false;
    }

    private void getChildren(Integer id, List<Type> types) {
        sb.append(",\"children\":[");
        int count =0;
        for (int i=0;i<types.size();i++){
            Type type=types.get(i);
            if (type.getPid()!=null&&type.getPid().equals(id)){
                count++;
                sb.append("{");
                sb.append("\"id\":").append(type.getId()).append(",\"pid\":").append(type.getPid()).append(",\"label\":\"").append(type.getName()).append("\"");
                getChildren(type.getId(),types);
                sb.append("},");
            }
        }
        if (count>0){
            sb.replace(sb.length()-1,sb.length(),"]");
        }else {
            sb.replace(sb.length()-13,sb.length(),"");
        }
    }
}
